<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="bg-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
    
    <!-- 登录卡片 -->
    <div class="login-card">
      <div class="login-header">
        <div class="logo">
          <div class="logo-icon">
            <el-icon><Lock /></el-icon>
          </div>
          <h1>权限管理系统</h1>
        </div>
        <p class="subtitle">安全、高效的企业级管理平台</p>
      </div>
      
      <div class="login-form-wrapper">
        <el-form 
          ref="loginFormRef" 
          :model="loginForm" 
          :rules="loginRules" 
          class="login-form"
        >
          <el-form-item prop="username">
            <div class="input-wrapper">
              <el-icon class="input-icon"><User /></el-icon>
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                size="large"
                class="custom-input"
              />
            </div>
          </el-form-item>
          
          <el-form-item prop="password">
            <div class="input-wrapper">
              <el-icon class="input-icon"><Lock /></el-icon>
              <el-input
                v-model="loginForm.password"
                :type="passwordVisible ? 'text' : 'password'"
                placeholder="请输入密码"
                size="large"
                class="custom-input"
                @keyup.enter="handleLogin"
              >
                <template #suffix>
                  <el-icon 
                    class="password-toggle" 
                    @click="passwordVisible = !passwordVisible"
                  >
                    <component :is="passwordVisible ? 'View' : 'Hide'" />
                  </el-icon>
                </template>
              </el-input>
            </div>
          </el-form-item>
          
          <div class="form-options">
            <el-checkbox v-model="rememberMe" class="remember-checkbox">
              记住密码
            </el-checkbox>
            <a href="javascript:void(0)" class="forgot-link">忘记密码？</a>
          </div>
          
          <el-button 
            :loading="loading" 
            type="primary" 
            class="login-btn"
            @click="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form>
        
        <!-- 演示账号信息 -->
        <div class="demo-info">
          <div class="demo-title">
            <el-icon><InfoFilled /></el-icon>
            <span>演示账号</span>
          </div>
          <div class="demo-accounts">
            <div class="demo-item" @click="fillAccount('admin', 'admin123')">
              <span class="role">管理员</span>
              <span class="account">admin / admin123</span>
            </div>
            <div class="demo-item" @click="fillAccount('test', 'test123')">
              <span class="role">测试用户</span>
              <span class="account">test / test123</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import { User, Lock, View, Hide, InfoFilled } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const loginFormRef = ref(null)
const loading = ref(false)
const passwordVisible = ref(false)
const rememberMe = ref(false)

const loginForm = reactive({
  username: 'admin',
  password: 'admin123'
})

const loginRules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}

// 填充演示账号
const fillAccount = (username, password) => {
  loginForm.username = username
  loginForm.password = password
}

const handleLogin = () => {
  loginFormRef.value.validate(async valid => {
    if (valid) {
      loading.value = true
      try {
        await userStore.login(loginForm)
        
        ElMessage({
          message: '登录成功',
          type: 'success'
        })
        
        const redirect = route.query.redirect || '/'
        await router.push(redirect)
        
      } catch (error) {
        console.error('登录失败:', error)
        ElMessage.error(error.message || '登录失败，请重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
  padding: 20px;
}

// 背景装饰
.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  
  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    animation: float 6s ease-in-out infinite;
    
    &.circle-1 {
      width: 200px;
      height: 200px;
      top: 10%;
      left: 10%;
      animation-delay: 0s;
    }
    
    &.circle-2 {
      width: 150px;
      height: 150px;
      top: 60%;
      right: 15%;
      animation-delay: 2s;
    }
    
    &.circle-3 {
      width: 100px;
      height: 100px;
      bottom: 20%;
      left: 20%;
      animation-delay: 4s;
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

// 登录卡片
.login-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 24px;
  padding: 40px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.2);
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
  }
}

// 登录头部
.login-header {
  text-align: center;
  margin-bottom: 40px;
  
  .logo {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 16px;
    
    .logo-icon {
      width: 64px;
      height: 64px;
      background: rgba(255, 255, 255, 0.2);
      border-radius: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 16px;
      backdrop-filter: blur(10px);
      border: 1px solid rgba(255, 255, 255, 0.3);
      
      .el-icon {
        font-size: 28px;
        color: white;
      }
    }
    
    h1 {
      font-size: 28px;
      font-weight: 700;
      color: white;
      margin: 0;
      text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    }
  }
  
  .subtitle {
    color: rgba(255, 255, 255, 0.8);
    font-size: 16px;
    margin: 0;
  }
}

// 表单样式
.login-form-wrapper {
  .login-form {
    .el-form-item {
      margin-bottom: 24px;
      
      .input-wrapper {
        position: relative;
        width: 100%;
        
        .input-icon {
          position: absolute;
          left: 16px;
          top: 50%;
          transform: translateY(-50%);
          color: rgba(255, 255, 255, 0.6);
          font-size: 18px;
          z-index: 2;
        }
        
        :deep(.custom-input) {
          width: 100%;
          
          .el-input__wrapper {
            width: 100% !important;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 16px;
            padding: 0 50px 0 48px;
            height: 56px;
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
            box-sizing: border-box;
            
            &:hover {
              background: rgba(255, 255, 255, 0.15);
              border-color: rgba(255, 255, 255, 0.3);
            }
            
            &.is-focus {
              background: rgba(255, 255, 255, 0.2);
              border-color: rgba(255, 255, 255, 0.4);
              box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.1);
            }
          }
          
          .el-input__inner {
            width: 100%;
            color: white;
            font-size: 16px;
            
            &::placeholder {
              color: rgba(255, 255, 255, 0.5);
            }
          }
        }
        
        .password-toggle {
          color: rgba(255, 255, 255, 0.6);
          cursor: pointer;
          transition: color 0.3s ease;
          
          &:hover {
            color: rgba(255, 255, 255, 0.8);
          }
        }
      }
    }
  }
  
  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 32px;
    
    .remember-checkbox {
      :deep(.el-checkbox__label) {
        color: rgba(255, 255, 255, 0.8);
        font-size: 14px;
      }
      
      :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
        background-color: rgba(255, 255, 255, 0.2);
        border-color: rgba(255, 255, 255, 0.4);
      }
      
      :deep(.el-checkbox__inner) {
        background-color: rgba(255, 255, 255, 0.1);
        border-color: rgba(255, 255, 255, 0.3);
      }
    }
    
    .forgot-link {
      color: rgba(255, 255, 255, 0.8);
      text-decoration: none;
      font-size: 14px;
      transition: color 0.3s ease;
      
      &:hover {
        color: white;
        text-decoration: underline;
      }
    }
  }
  
  .login-btn {
    width: 100%;
    height: 56px;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0.1));
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 16px;
    color: white;
    font-size: 16px;
    font-weight: 600;
    backdrop-filter: blur(10px);
    transition: all 0.3s ease;
    
    &:hover {
      background: linear-gradient(135deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.2));
      border-color: rgba(255, 255, 255, 0.4);
      transform: translateY(-2px);
      box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
}

// 演示信息
.demo-info {
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  
  .demo-title {
    display: flex;
    align-items: center;
    gap: 8px;
    color: rgba(255, 255, 255, 0.8);
    font-size: 14px;
    margin-bottom: 16px;
    
    .el-icon {
      font-size: 16px;
    }
  }
  
  .demo-accounts {
    display: flex;
    flex-direction: column;
    gap: 12px;
    
    .demo-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 16px;
      background: rgba(255, 255, 255, 0.05);
      border: 1px solid rgba(255, 255, 255, 0.1);
      border-radius: 12px;
      cursor: pointer;
      transition: all 0.3s ease;
      
      &:hover {
        background: rgba(255, 255, 255, 0.1);
        border-color: rgba(255, 255, 255, 0.2);
        transform: translateY(-1px);
      }
      
      .role {
        color: rgba(255, 255, 255, 0.9);
        font-size: 14px;
        font-weight: 500;
      }
      
      .account {
        color: rgba(255, 255, 255, 0.7);
        font-size: 13px;
        font-family: 'Courier New', monospace;
      }
    }
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-container {
    padding: 16px;
  }
  
  .login-card {
    padding: 32px 24px;
    border-radius: 20px;
  }
  
  .login-header {
    margin-bottom: 32px;
    
    .logo {
      .logo-icon {
        width: 56px;
        height: 56px;
        
        .el-icon {
          font-size: 24px;
        }
      }
      
      h1 {
        font-size: 24px;
      }
    }
    
    .subtitle {
      font-size: 14px;
    }
  }
  
  .demo-accounts {
    .demo-item {
      flex-direction: column;
      align-items: flex-start;
      gap: 4px;
    }
  }
}

// 移除Element Plus默认样式
:deep(.el-form-item__error) {
  color: rgba(255, 255, 255, 0.8);
  background: rgba(255, 0, 0, 0.1);
  padding: 4px 8px;
  border-radius: 6px;
  backdrop-filter: blur(10px);
}
</style>